package com.yxhl.zoume.core.tripsmgmt.ui.fragment.dialog;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yxhl.zoume.R;
import com.yxhl.zoume.utils.DensityUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class GetTicketCodeDialogFragment extends DialogFragment {

    public static final String ARGUMENT_VERIFY_CODE_KEY = "argument_verify_code_key";

    @BindView(R.id.ll_get_ticket_dialog_container)
    LinearLayout mTicketDialogContainer;

    @BindView(R.id.tv_get_ticket_dialog_title)
    TextView titleTv;

    @BindView(R.id.tv_get_ticket_dialog_confirm)
    TextView confirmTv;

    private String mVerifyCode;

    public GetTicketCodeDialogFragment() {
        // Required empty public constructor
    }

    public static GetTicketCodeDialogFragment newInstance(String verifyCode) {
        GetTicketCodeDialogFragment codeDialogFragment = new GetTicketCodeDialogFragment();
        Bundle bundle = new Bundle();
        if (verifyCode != null) {
            bundle.putString(ARGUMENT_VERIFY_CODE_KEY, verifyCode);
        }
        codeDialogFragment.setArguments(bundle);
        return codeDialogFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NO_TITLE, 0);
        Bundle arguments = getArguments();
        if (arguments != null) {
            mVerifyCode = arguments.getString(ARGUMENT_VERIFY_CODE_KEY);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_get_ticket_code_dialog, container);
        ButterKnife.bind(this, view);

        setShowContent(mVerifyCode);
        confirmTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        return view;
    }

    private void setShowContent(String verifyCode) {
        if (!TextUtils.isEmpty(verifyCode)) {
            String[] strings = verifyCode.trim().split(",");
            if (strings != null) {
                int length = strings.length;
                if (length > 0) {
                    for (int i = 0; i < length; i++) {
                        TextView verifyCodeTv = new TextView(getContext());
                        ViewGroup.MarginLayoutParams marginLayoutParams =
                                new ViewGroup.MarginLayoutParams(
                                        ViewGroup.LayoutParams.WRAP_CONTENT,
                                        ViewGroup.LayoutParams.WRAP_CONTENT);
                        marginLayoutParams.setMargins(
                                0,
                                DensityUtils.px2dip(getContext(), 20),
                                0,
                                DensityUtils.px2dip(getContext(), 20));
                        verifyCodeTv.setLayoutParams(marginLayoutParams);
                        verifyCodeTv.setText(strings[i]);
                        verifyCodeTv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
                        verifyCodeTv.setTextColor(getResources().getColor(R.color.font_color_orange));

                        mTicketDialogContainer.addView(verifyCodeTv);
                    }
                }
            }
        }
    }
//    android 原生的对话框
//    @Override
//    public Dialog onCreateDialog(Bundle savedInstanceState) {
//        super.onCreateDialog(savedInstanceState);
//        AlertDialog.Builder show = new AlertDialog.Builder(getContext());
//         show.setTitle("提示")
//                .setMessage(mVerifyCode)
//                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        dialog.dismiss();
//                    }
//                });
//
//        return show.create();
//    }
}
